﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Master_DocContractUpdForm
 * 类型：       控制层
 * 功能说明：   销售资料维护登记更新页面
 * 作者：       章 昕
 * 创建日期：   2007/08/20
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Master.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Utils;
using Shpm.System.Web.Control;
using log4net;
using System.Text.RegularExpressions;
using Shpm.System;
using Shpm.App.Commons.Utils;

public partial class App_Master_DocContractUpdForm : System.Web.UI.Page
{

    // 合同内容content
    private string docContentHtml;

    public string DocContentHtml
    {
        get { return docContentHtml; }
        set { docContentHtml = value; }
    }

    protected static ILog log = LogManager.GetLogger(typeof(App_Master_DocContractUpdForm));
    // init dao
    DocContractDao dao = new DocContractDao();

    // page load
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {   // docid from previous page
            if (Context.Items["docId"] == null)
                this.ViewState["DOC_ID"] = "";
            else
                this.ViewState["DOC_ID"] = Context.Items["docId"].ToString();
            // init bind controls
            rblDocType.Items.Clear();
            bindCommonDicCats();

            // edit
            if (this.ViewState["DOC_ID"].ToString().Length > 0)
            {
                bindSelectDocData(this.ViewState["DOC_ID"].ToString());
            }
            if (this.rblDocType.Items.Count > 0)
            {
                this.rblDocType.Items[0].Selected = true;
            }

            if (Context.Items["docType"] != null &&
                Context.Items["docTitle"] != null &&
                Context.Items["docContent"] != null &&
                Context.Items["docIdSrh"] != null)
            {
                this.ViewState["docType"] = Context.Items["docType"].ToString();
                this.ViewState["docTitle"] = Context.Items["docTitle"].ToString();
                this.ViewState["docContent"] = Context.Items["docContent"].ToString();
                this.ViewState["docIdSrh"] = Context.Items["docIdSrh"].ToString();
                this.ViewState["ParamPageIndex"] = Context.Items["ParamPageIndex"].ToString();
            }
        }
        else
        {
            string htmlDoc = Convert.ToString(Request.Form["FCKeditor1"]);
            if (htmlDoc == null) htmlDoc = "";
            htmlDoc = Regex.Replace(htmlDoc, "\\n|\\r|\\t|\\v", ""); //^\s*|\s*$
            //this.ViewState["HTML_DOC"] = htmlDoc;
            docContentHtml = Regex.Replace(htmlDoc, "'", "\\'");
        }

    }

    // bind common catalog data  
    private void bindCommonDicCats()
    {
        CommonDictionaryBean beanComDict = new CommonDictionaryBean();
        IList lstCat = null;

        // 资料类别
        log.Fatal("\n ***** get SALE_DOC_TYPE catalog *****");
        beanComDict.Catalog = "SALE_DOC_TYPE";
        lstCat = dao.FindByCatalog(beanComDict);
        bindDataToControl("rblDocType", lstCat);
    }

    // bind select doc data by docid
    private void bindSelectDocData(string docId)
    {
        DocContractBean beanDC = new DocContractBean();
        beanDC.DocId = int.Parse(docId);
        beanDC = dao.FindByDocContract(beanDC);
        if (beanDC != null)
        {
            this.lblDocId.Text = beanDC.DocId.ToString();
            this.txtTitle.Text = beanDC.Title;
            this.txtPubDate.Text = DateUtils.convertToString(beanDC.PubDate);
            this.txtAuthor.Text = beanDC.Author;
            this.docContentHtml = beanDC.Content;
            if (this.rblDocType.Items.Count > 0)
            {
                ListItem selectItem = this.rblDocType.Items.FindByValue(beanDC.Type);
                if (selectItem != null) selectItem.Selected = true;
                else this.rblDocType.Items[0].Selected = true;
            }
        }
    }

    // 登入按钮
    protected void btnOk_Click(object sender, EventArgs e)
    {
        try
        {
            if (comValidatUpd.DoValidator())
            {
                DocContractDao dao = new DocContractDao();
                DocContractBean beanDC = new DocContractBean();
                SysUserManager manager = new SysUserManager(Context);
                SysUserBean sysUserBean = manager.getSysUserBean();
                MstSysUserBean mstSysUserBean =
                    sysUserBean == null ? null : sysUserBean.getMstSysUserBean();
                MstEmployeeBean mstEmpBean =
                    sysUserBean == null ? null : sysUserBean.getEmployeeBean();
                string loginId = mstSysUserBean == null ? "" : mstSysUserBean.SysUserId;                
                
                beanDC.Title = this.txtTitle.Text;
                //beanDC.Content = ;
                beanDC.Type = this.rblDocType.SelectedValue;
                // original html text
                string htmlDoc = Request.Form["FCKeditor1"];
                htmlDoc = Regex.Replace(htmlDoc, "\\n|\\r|\\t|\\v", ""); //^\s*|\s*$
                string tmpKeyword = htmlDoc;
                beanDC.Content = Regex.Replace(htmlDoc, "'", "\\'");
                // key word html text
                string htmlKeyWord = Regex.Replace(tmpKeyword, "<[^><]*>", "");
                htmlKeyWord = Server.HtmlDecode(htmlKeyWord);
                beanDC.ContentKeyword = htmlKeyWord;
                if (this.txtPubDate.Text == null || this.txtPubDate.Text == "")
                    beanDC.PubDate = DateTime.Now;
                else
                    beanDC.PubDate = DateUtils.convertToDate(this.txtPubDate.Text);
                beanDC.Author = this.txtAuthor.Text;
                beanDC.UserId = loginId;
                docContentHtml = beanDC.Content;

                if (this.ViewState["DOC_ID"].ToString().Length > 0)
                {
                    beanDC.DocId = int.Parse(this.ViewState["DOC_ID"].ToString());
                    log.Info("***** Start UPDATE doccontract *****");
                    dao.UpdateDoc(beanDC);
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString();
                }
                else
                {
                    log.Info("***** Start INSERT doccontract *****");
                    Object retKey = dao.InsertDoc(beanDC);
                    if (retKey != null)
                    {
                        this.ViewState["DOC_ID"] = retKey.ToString();
                        lblDocId.Text = retKey.ToString();
                        ((Design_MasterPage)this.Master).InfoMessage =
                            HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
        }

    }

    // 删除按钮
    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            if (this.ViewState["DOC_ID"].ToString().Length > 0)
            {
                // delete
                DocContractDao dao = new DocContractDao();
                DocContractBean beanDC = new DocContractBean();
                beanDC.DocId = int.Parse(this.ViewState["DOC_ID"].ToString());
                CheckDataExistsAttribute chkDataExtAttr = new CheckDataExistsAttribute(
                        dao, DocContractDao.EXISTS_DOC_CONTRACT, beanDC);
                chkDataExtAttr.Code = "E0203";
                ComAbsParamsAttribute paramAttr = new ComAbsParamsAttribute();
                paramAttr.Parameter = "文件编号";
                chkDataExtAttr.Parameters.Add(paramAttr);
                comValidatUpd.CheckItems.Add(chkDataExtAttr);

                if (comValidatUpd.DoValidator())
                {
                    dao.DeleteDoc(beanDC);
                    ((Design_MasterPage)this.Master).InfoMessage =
                        HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0003").ToString();
                }
            }

            resetFormControls();
        }
        catch (Exception ex)
        {
            log.Fatal(ex.StackTrace);
            log.Fatal(ex.Message);
        }

    }

    // 返回按钮
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Context.Items["PAGE_BACK"] = "DocContractUpdForm";
        Context.Items["docType"] = this.ViewState["docType"];
        Context.Items["docTitle"] = this.ViewState["docTitle"];
        Context.Items["docContent"] = this.ViewState["docContent"];
        Context.Items["docIdSrh"] = this.ViewState["docIdSrh"];
        Context.Items["ParamPageIndex"] = this.ViewState["ParamPageIndex"];
        // Server.Transfer("DocContractSrhForm.aspx", true);
        ControllerUtils.Transfer(this.Context, "~/App/Master/DocContractSrhForm.aspx", true);
    }

    // init controls
    private void resetFormControls()
    {
        // reset textbox
        this.lblDocId.Text = "";
        this.txtTitle.Text = "";
        this.txtPubDate.Text = "";
        this.txtAuthor.Text = "";
        this.docContentHtml = "";
        this.ViewState["DOC_ID"] = "";
    }

    // bind db data to selected controls
    private void bindDataToControl(string ControlId, IList LstData)
    {
        if (LstData == null || LstData.Count == 0) return;

        foreach (CommonDictionaryBean bean in LstData)
        {
            if (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is DropDownList)
            {
                ((DropDownList)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
            else if ((this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is CheckBoxList)
                || (this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId) is RadioButtonList))
            {
                ((ListControl)this.Master.FindControl("ContentPlaceHolder1").FindControl(ControlId)).Items.Add(new ListItem(bean.Content, bean.DictNo.ToString()));
            }
        }
    }
}
